package com.fr.base.xml;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public abstract class XMLWriterHelper
{
  public static XmlStartTag startTag(String paramString)
  {
    return new XmlStartTag(paramString, null);
  }

  public static XmlStartTag completeTagWithNoChildNode(String paramString)
  {
    return new XmlStartTag(paramString, true, null);
  }

  public static void startTagWithNoAttribute(PrintWriter paramPrintWriter, String paramString)
  {
    paramPrintWriter.print("<" + paramString + ">");
  }

  public static void endTag(PrintWriter paramPrintWriter, String paramString)
  {
    paramPrintWriter.print("</" + paramString + ">");
  }

  public static class XmlStartTag
  {
    private String tagName;
    private Map attributes;
    private boolean withNoChildNode;

    private XmlStartTag(String paramString)
    {
      this.withNoChildNode = false;
      this.tagName = paramString;
    }

    private XmlStartTag(String paramString, boolean paramBoolean)
    {
      this(paramString);
      this.withNoChildNode = paramBoolean;
    }

    public XmlStartTag attr(String paramString1, String paramString2)
    {
      if (paramString2 == null)
        return this;
      if (this.attributes == null)
        this.attributes = new HashMap();
      this.attributes.put(paramString1, paramString2);
      return this;
    }

    public XmlStartTag attr(String paramString1, String paramString2, String paramString3)
    {
      if ((paramString2 == null) || (paramString2.equals(paramString3)))
        return this;
      return attr(paramString1, paramString2);
    }

    public XmlStartTag attr(String paramString, boolean paramBoolean)
    {
      return attr(paramString, Boolean.toString(paramBoolean));
    }

    public XmlStartTag attr(String paramString, boolean paramBoolean1, boolean paramBoolean2)
    {
      if (paramBoolean1 == paramBoolean2)
        return this;
      return attr(paramString, paramBoolean1);
    }

    public XmlStartTag attr(String paramString, byte paramByte)
    {
      return attr(paramString, Byte.toString(paramByte));
    }

    public XmlStartTag attr(String paramString, byte paramByte1, byte paramByte2)
    {
      if (paramByte1 == paramByte2)
        return this;
      return attr(paramString, paramByte1);
    }

    public XmlStartTag attr(String paramString, int paramInt)
    {
      return attr(paramString, Integer.toString(paramInt));
    }

    public XmlStartTag attr(String paramString, int paramInt1, int paramInt2)
    {
      if (paramInt1 == paramInt2)
        return this;
      return attr(paramString, paramInt1);
    }

    public XmlStartTag attr(String paramString, double paramDouble)
    {
      return attr(paramString, Double.toString(paramDouble));
    }

    public XmlStartTag attr(String paramString, double paramDouble1, double paramDouble2)
    {
      if (paramDouble1 == paramDouble2)
        return this;
      return attr(paramString, paramDouble1);
    }

    public void write(PrintWriter paramPrintWriter)
    {
      paramPrintWriter.write("<" + this.tagName);
      paramPrintWriter.write(attributeBuffer().toString());
      if (this.withNoChildNode)
        paramPrintWriter.write("/>");
      else
        paramPrintWriter.write(">");
    }

    private StringBuffer attributeBuffer()
    {
      StringBuffer localStringBuffer = new StringBuffer();
      if (this.attributes != null)
      {
        Iterator localIterator = this.attributes.entrySet().iterator();
        while (localIterator.hasNext())
        {
          Map.Entry localEntry = (Map.Entry)localIterator.next();
          localStringBuffer.append(" ").append(localEntry.getKey()).append("=\"").append(BaseXMLUtils.xmlAttrEncode(localEntry.getValue().toString())).append("\"");
        }
      }
      return localStringBuffer;
    }

    public String toString()
    {
      StringWriter localStringWriter = new StringWriter();
      write(new PrintWriter(localStringWriter));
      return localStringWriter.getBuffer().toString();
    }

    XmlStartTag(String paramString, XMLWriterHelper.1 param1)
    {
      this(paramString);
    }

    XmlStartTag(String paramString, boolean paramBoolean, XMLWriterHelper.1 param1)
    {
      this(paramString, paramBoolean);
    }
  }
}